
// //监听事件 on(eventName,callback)
// //移除监听 off(eventName)

// const Events = require('events');
// const eventEmiter = new Events();
// eventEmiter.setMaxListeners(0);//解除事件回调数量限制(默认同一事件可设置最多10个回调)
// // eventEmiter.off = eventEmiter.removeAllListeners;
// eventEmiter.off = eventEmiter.removeListener;
// // module.exports = eventEmiter;

let events = [];
module.exports = {
    on(eventName, fun, target) {
        events.push({ eventName: eventName, fun: fun, target: target });
    },
    off(eventName, fun, target) {
        events = events.filter(item => { return item.eventName != eventName || item.fun !== fun || item.target != target; });
    },
    emit(eventName, ...args) {
        let currentEvents = events.filter(item => { return item.eventName == eventName });
        currentEvents.map(item => {
            item.fun.apply(item.target, args);
        });
        events = events.filter(item => { return item.eventName != eventName || (item.eventName == eventName && !item.isOnce) });
    },
    removeAllListeners(eventName) {
        if(typeof(eventName) == 'object'){
            events = events.filter(item => { return item.target != eventName });
        } else {
            events = events.filter(item => { return item.eventName != eventName });
        }
    },
    once(eventName, fun, target) {
        events.push({ eventName: eventName, fun: fun, target: target, isOnce: true });
    }
};